/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actions;

import edu.ipfw.cs590.cheat.AssignmentManagementInterface;
import edu.ipfw.cs590.cheat.AssignmentManagementService;
import edu.ipfw.cs590.cheat.util.Authentication;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author SafarTimuro
 */
public class DeleteAssignment extends org.apache.struts.action.Action {
    /* forward name="success" path="" */

    private final static String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession(false);

        if (session != null) {
            try { // Call Web Service Operation
                AssignmentManagementService service = new AssignmentManagementService();
                AssignmentManagementInterface port = service.getAssignmentManagementSOAP();
                // TODO initialize WS operation arguments here
                edu.ipfw.cs590.cheat.RemoveAssignment parameters = new edu.ipfw.cs590.cheat.RemoveAssignment();
                parameters.setUUID(request.getParameter("id"));
                String token = (String) session.getAttribute(Authentication.SESSION_NAME_TOKEN);

                //Delete the assignment
                if (port.removeAssignment(parameters, token).isResult()) {
                    //Store message for redisplay
                    ActionMessages errors = new ActionMessages();
                    errors.add(ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage("success.service.deleteAssignment", request.getParameter("id")));
                    this.saveMessages(request, errors);

                    return mapping.findForward(SUCCESS);
                } else {
                    //Something bad happened on the webservice side fail.
                    ActionMessages errors = new ActionMessages();
                    errors.add(ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage("errors.service.deleteAssignment", request.getParameter("id")));
                    this.saveErrors(request, errors);

                    return mapping.getInputForward();
                }
            } catch (Exception exc) {
                //Something bad happened on the webservice side fail.
                ActionMessages errors = new ActionMessages();
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("errors.service.error", "Delete Assignment", exc.getMessage()));
                this.saveErrors(request, errors);

                //Return failure.
                return mapping.getInputForward();
            }
        }

        //Something bad happened on the webservice side fail.
        ActionMessages errors = new ActionMessages();
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.form.invalid"));
        this.saveErrors(request, errors);

        return new ActionForward(mapping.getInput());
    }
}
